package hero.nio;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.channels.SocketChannel;
import java.net.InetSocketAddress;

public class BaiduReader {

    public static void main(String[] args) throws Exception {
        Charset charset  = Charset.forName("GBK");
        InetSocketAddress address = new InetSocketAddress("www.baidu.com",80);
        SocketChannel channel = SocketChannel.open(address);

        channel.write(charset.encode("GET / HTTP/1.1\r\n\r\n"));
        //channel.write(charset.encode("GET " + "/ HTTP/1.1" + "\r\n\r\n")); 

        ByteBuffer buffer = ByteBuffer.allocate(2048);
        while (channel.read(buffer) != -1) {
            buffer.flip();
            System.out.println(charset.decode(buffer));
            buffer.clear();
        }
        channel.close();
    }

}
